package org.crystal.tradecraft.serialization;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.crystal.tradecraft.model.binance.enumeration.FilterType;
import org.crystal.tradecraft.model.binance.market.filter.BinanceFilter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BinanceFiltersDeserializer extends JsonDeserializer<List<? extends BinanceFilter>> {

    @Override
    public List<? extends BinanceFilter> deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
        JsonNode jsonNode = jsonParser.readValueAsTree();
        ObjectCodec codec = jsonParser.getCodec();
        List<BinanceFilter> filters = new ArrayList<>();

        for (JsonNode node : jsonNode) {
            Class<? extends BinanceFilter> filterClass = FilterType.valueOf(node.get(BinanceFilter.FILTER_TYPE_NAME_STR).asText()).getFilterClass();
            filters.add(codec.treeToValue(node, filterClass));
        }
        return filters;
    }
}
